package FreebaseDumps;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

import DatabaseManager.Table;

public class FileDumper {
	
	/**
	 * dumps file_name to table
	 * @param file_name - name of the file to be dumped
	 * @param table - the table we dump the file to
	 * @param relvantColumnNumbers - numbers of columns in the table represented
	 * by the file that we won't to dump to our table
	 * @param isCharacterDumper - true <=> this is used for the fictional_character.tsv file
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws SQLException
	 */
	public void DumpFileToTable(String file_name, Table table,
			int[] relvantColumnNumbers, boolean isCharacterDumper)
			throws FileNotFoundException, IOException, SQLException {
		TSVParser parser = new TSVParser(relvantColumnNumbers, isCharacterDumper);
		String[] tokens;

		// open file
		parser.OpenFile(file_name);

		// get first row and create table
		tokens = parser.GetNextQuad(); // just gets read of the header line
		// fillup table

		// get next quad data
		tokens = parser.GetNextQuad();

		while (true) {
			// insert to table
			if (tokens == null)
				break;
			table.InsertTable(tokens);
			// get next quad data
			tokens = parser.GetNextQuad();
			
		}
		// while loop ended -> finished running over all tokens
		table.finalizeInsertTable();
		// table.InsertTable(null, true);

		parser.CloseFile();
	}
}
